//
//  Tools.swift
//  Cloud
//
//  Created by Mac on 2021/6/2.
//

import Foundation

class Tool: NSObject {
    class func like(radio: RadioResponseModelRadio){
        let documentDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first! as NSString
        let filePath = documentDirectory.appendingPathComponent("favorite.plist")
        let fm = FileManager.default
        
        if !fm.fileExists(atPath: filePath) {
            do {
                let array = [radio.toJSON()] as! NSArray
                try array.write(to: URL(fileURLWithPath: filePath))
            } catch let error {
                debugPrint(error.localizedDescription)
            }
        }else{
            let array = NSMutableArray(contentsOfFile: filePath)
            if array?.count ?? 0 > 0 {
                for index in 0...(array?.count ?? 1) - 1 {
                    let objectJson = array?[index] as! [String:Any]
                    let object = RadioResponseModelRadio.deserialize(from: objectJson)
                    if object?.radio_id == radio.radio_id {
                        break
                    }
                }
            }
            array?.add(radio.toJSON())
            do {
                try array?.write(to: URL(fileURLWithPath: filePath))
            } catch let error {
                debugPrint(error.localizedDescription)
            }
        }
    }
    
    class func unlike(radio: RadioResponseModelRadio){
        let documentDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first! as NSString
        let filePath = documentDirectory.appendingPathComponent("favorite.plist")
        let array = NSMutableArray(contentsOfFile: filePath)
        var currentIndex = 0
        if array?.count ?? 0 > 0 {
            for index in 0...(array?.count ?? 1) - 1 {
                let objectJson = array?[index] as! [String:Any]
                let object = RadioResponseModelRadio.deserialize(from: objectJson)
                if object?.radio_id == radio.radio_id {
                    currentIndex = index
                    break
                }
            }
        }
        array?.removeObject(at: currentIndex)
        do {
            try array?.write(to: URL(fileURLWithPath: filePath))
        } catch let error {
            debugPrint(error.localizedDescription)
        }
    }
    
    class func list() -> [RadioResponseModelRadio]{
        let documentDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first! as NSString
        let filePath = documentDirectory.appendingPathComponent("favorite.plist")
        let array = NSMutableArray(contentsOfFile: filePath)
        var tempArray = [RadioResponseModelRadio]()
        let arrayCount = array?.count ?? 0
        if arrayCount > 0 {
            for index in 0...arrayCount - 1 {
                let dict = array?[index] as! NSDictionary
                let model = RadioResponseModelRadio.deserialize(from: dict)
                tempArray.append(model ?? RadioResponseModelRadio())
            }
        }
        return tempArray
    }
    
    class func isLike(radio: RadioResponseModelRadio) -> Bool{
        let tempArray = Tool.list()
        var isLike = false
        for model in tempArray {
            if model.radio_id == radio.radio_id {
                isLike = true
            }
        }
        return isLike
    }
}
